//
//  LTHmEcgGridView.m
//  TH_HM_CombineSDKTestDemo
//
//  Created by lanzhongping on 2020/11/23.
//  Copyright © 2020 None. All rights reserved.
//

#import "LTHmEcgGridView.h"
#import "UIView+LayoutDirection.h"

CGFloat const ECG_GRID_LINE_WIDTH = 0.4f; // 1 pt
#define GRIDLINE_COLOR_DEFAULT ([UIColor colorWithRed:74/255.0f green:74/255.0f \
                                blue:74/255.0f alpha:0.7f])

@implementation LTHmEcgGridView
{
    __weak CAShapeLayer *_gridLayer;
    __weak CAShapeLayer *_gridLayerBold; // 每五个
//    CGFloat _bottomSpaceForSecond; // 底部留白为了显示秒数
    NSMutableArray<UILabel *> *_secondLabels;
    const NSArray<NSNumber *> * _xSpeedArray;

}

-(instancetype)init {
    if (self = [super init]) {
        
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = NO;
        _xSpeedArray = @[@(25.0f), @(50.0f)];           // cannot modify
        _bottomSpaceForSecond = 0;
        if (!self.gridLineColor) {
            self.gridLineColor = GRIDLINE_COLOR_DEFAULT;
        }
    }
    return self;
}

-(void)setShowSeconds:(BOOL)showSeconds
{
    _showSeconds = showSeconds;
    _bottomSpaceForSecond = _showSeconds ? 20.f : 0.f ;
}



-(void)layoutSubviews
{
    [super layoutSubviews];
    [self drawGrid];
}

-(CGFloat)historyTypeHeight
{
    if (self.showSeconds) {
        return 30 * TRANS_MM_TO_PT + _bottomSpaceForSecond;
    }
    
    return  30 * TRANS_MM_TO_PT;
    
}

-(CGFloat)lengthBySquareCount:(NSUInteger)count {
    return  count * TRANS_MM_TO_PT;
}


/// 整数倍的格子长度
/// - Parameter length:
+(CGFloat)integetLengthFrom:(CGFloat)length {
    
    NSInteger horizontalLineCount = LINE_OR_BAR_COUNTS(length);
    
    horizontalLineCount = horizontalLineCount /5 * 5; // 5个格子为单位
    
    return  horizontalLineCount * TRANS_MM_TO_PT;
}

//画网格  draw grid
- (void)drawGrid
{
    if (_gridLayer) {
        [_gridLayer removeFromSuperlayer];
    }
    if (_gridLayerBold) {
        [_gridLayerBold removeFromSuperlayer];
    }
    [_secondLabels enumerateObjectsUsingBlock:^(UILabel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    _secondLabels = nil;
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    
    //Horizontal line count (横线数量)
    NSInteger horizontalLineCount = LINE_OR_BAR_COUNTS(height- _bottomSpaceForSecond);
    if (self.showSeconds) {
        horizontalLineCount = 30;
    }
    
    //vertical bar count (竖线数量)
    NSInteger vertialBarCount = LINE_OR_BAR_COUNTS(width);
    
    // draw Horizontal line
    UIBezierPath *gridPath = [[UIBezierPath alloc] init];
    
    // draw Horizontal line
    UIBezierPath *gridPathBold = [[UIBezierPath alloc] init];
    
   
    int sep =  ([_xSpeedArray[self.xSpeedType] intValue] / 25) ;
    // 画横线
    for (int i = 0 ; i <= horizontalLineCount; i++) {
        [gridPath moveToPoint:CGPointMake(0,  i * TRANS_MM_TO_PT)];
        [gridPath addLineToPoint:CGPointMake(width, i * TRANS_MM_TO_PT)];
        if (i % (5 * 1) == 0) { // if (i % 5 == 0)
            [gridPathBold moveToPoint:CGPointMake(0,  i * TRANS_MM_TO_PT)];
            [gridPathBold addLineToPoint:CGPointMake(width, i * TRANS_MM_TO_PT)];
        }
        
    }
    
    //draw vertical bar 竖线
    _secondLabels = [NSMutableArray new];
    CGFloat vend = height;
    if (self.showSeconds) {
        vend = horizontalLineCount * TRANS_MM_TO_PT;
    }
    for (int i = 0 ; i <= vertialBarCount; i++) {
        
        
        [gridPath moveToPoint:CGPointMake(i * TRANS_MM_TO_PT, 0)];
        [gridPath addLineToPoint:CGPointMake(i * TRANS_MM_TO_PT, vend)];
        if (i % (5 * 1) == 0) {
            [gridPathBold moveToPoint:CGPointMake(i * TRANS_MM_TO_PT, 0)];
            if (self.showSeconds && i % (25* sep) == 0) {
                [gridPathBold addLineToPoint:CGPointMake(i * TRANS_MM_TO_PT, height)];

            } else {
                
                [gridPathBold addLineToPoint:CGPointMake(i * TRANS_MM_TO_PT, vend)];
            }
        }
        
        if (self.showSeconds && i % (25 * sep) == 0) {
            NSString *timeStr = [NSString stringWithFormat:@"%d%@", (i / (25 * sep)), self.secondUnitString]; // 第几秒
            UILabel *secLbl = [UILabel new];
            secLbl.textAlignment = self.directionFromLeft ? NSTextAlignmentLeft : NSTextAlignmentRight;
            secLbl.font = [UIFont systemFontOfSize:12];
            secLbl.textColor = self.gridLineColor;
            secLbl.text = timeStr;
            [secLbl sizeToFit];
            [_secondLabels addObject:secLbl];
            [self addSubview:secLbl];
            secLbl.frame = CGRectMake(i * TRANS_MM_TO_PT + 1, height- _bottomSpaceForSecond, secLbl.bounds.size.width, _bottomSpaceForSecond);
        }
        
    }
    
    CAShapeLayer *gridLayer = [CAShapeLayer layer];
    gridLayer.frame = self.bounds;
    gridLayer.lineWidth = ECG_GRID_LINE_WIDTH;
    if (!self.gridLineColor) {
        self.gridLineColor = GRIDLINE_COLOR_DEFAULT;
    }
    gridLayer.strokeColor = self.gridLineColor.CGColor;
    gridLayer.fillColor = UIColor.clearColor.CGColor;
    gridLayer.lineCap = kCALineCapSquare;
    gridLayer.path = gridPath.CGPath;
    [self.layer addSublayer:gridLayer];
    _gridLayer = gridLayer;
    
    
    CAShapeLayer *gridLayerBold = [CAShapeLayer layer];
    gridLayerBold.frame = self.bounds;
    gridLayerBold.lineWidth = ECG_GRID_LINE_WIDTH * 4;
//    if (!self.gridLineColor) {
//        self.gridLineColor = GRIDLINE_COLOR_DEFAULT;
//    }
    gridLayerBold.strokeColor = self.gridLineColor.CGColor;
    gridLayerBold.fillColor = UIColor.clearColor.CGColor;
    gridLayerBold.lineCap = kCALineCapSquare;
    gridLayerBold.path = gridPathBold.CGPath;
    [self.layer addSublayer:gridLayerBold];
    _gridLayerBold = gridLayerBold;
    
}

@end
